package com.bien.springboot.controller;

import com.bien.springboot.base.controller.BaseController;
import com.bien.springboot.entity.UserEntity;
import com.bien.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @RequestMapping("/getUsers")
    public List<UserEntity> getUsers() {
        List<UserEntity> users = userService.queryList();
        return users;
    }

    @RequestMapping("/getUser")
    public UserEntity getUser(Long id) {
        UserEntity user = userService.getModelById(id);
        return user;
    }

    @RequestMapping("/add")
    public void save(UserEntity user) {
        try {
            userService.insert(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "update")
    public void update(UserEntity user) {
        try {
            userService.updateByObject(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/delete/{id}")
    public void delete(@PathVariable("id") Long id) {
        try {
            userService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}